package com.tykboot.caches;

import com.alibaba.fastjson.JSON;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

/**
 * @author LiuYu
 * @date 2018/1/7
 */
@Component
@ConditionalOnProperty(name = "spring.cache.type", havingValue = "ehcache")
public class EhCacheService implements BaseCacheService {

    @Autowired
    private EhCacheCacheManager ehCacheCacheManager;

    @Override
    public Object get(Object key) {
        Cache cache = ehCacheCacheManager.getCacheManager().getCache(DEFAULT_CACHE);
        if (cache != null) {
            Element element = cache.get(key);
            if (element != null) {
                return element.getObjectValue();
            }
        }
        return null;
    }

    @Override
    public <T> T get(Object key, Class<T> clazz) {
        Cache cache = ehCacheCacheManager.getCacheManager().getCache(DEFAULT_CACHE);
        if (cache != null) {
            Element element = cache.get(key);
            if (element != null) {
                Object value = element.getObjectValue();
                return JSON.parseObject(JSON.toJSONString(value), clazz);
            }
        }
        return null;
    }

    @Override
    public void put(Object key, Object value) {
        Cache cache = ehCacheCacheManager.getCacheManager().getCache(DEFAULT_CACHE);
        if (cache != null) {
            cache.put(new Element(key, value, 0, 2 * 60 * 60));
        }
    }

    @Override
    public void put(Object key, Object value, int timeToLiveSeconds) {
        Cache cache = ehCacheCacheManager.getCacheManager().getCache(DEFAULT_CACHE);
        if (cache != null) {
            cache.put(new Element(key, value, 0, timeToLiveSeconds));
        }
    }

    @Override
    public boolean remove(Object key) {
        Cache cache = ehCacheCacheManager.getCacheManager().getCache(DEFAULT_CACHE);
        if (cache != null) {
            return cache.remove(key);
        }
        return false;
    }
}
